/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-03-27
 * Time: 21:36
 */
public class MList {

    class ListNode {
        public int val;//存放数据
        public ListNode next;//存放下一个节点引用

        public ListNode(int val) {
            this.val = val;
        }

    }

    public ListNode head;//头节点

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;

    }



    public void listDisplay() {
        ListNode pcur = head;
        while(pcur != null) {
            System.out.print(pcur.val + " ");
            pcur = pcur.next;
        }
        System.out.println();
    }


    public int size() {
        int count = 0;
        ListNode pcur = head;
        while(pcur != null) {
            count++;
            pcur = pcur.next;
        }
        return count;
    }


    public void addFirst(int val) {
        ListNode addnode = new ListNode(val);
        addnode.next = head;
        head = addnode;
    }

    public void addLast(int val) {
        ListNode addnode = new ListNode(val);
        if(head == null) {
            head = addnode;
            return;
        }
        ListNode pcur = head;
        while(pcur.next != null) {
            pcur = pcur.next;
        }
        pcur.next = addnode;
    }


     public void addIndex(int val,int pos) {
        //判断pos是否合法
         try{
             check(pos);
         }catch (PosIllegal posIllegal) {
             posIllegal.printStackTrace();
         }

         //如果pos == 0就是头插
         if(pos == 0) {
             addFirst(val);
             return;
         }

         //如果pos == this.size()
         if(pos == this.size()) {
             addLast(val);
             return;
         }


         //pos > 0 && pos < this.size()
         ListNode newnode =new ListNode(val);
         ListNode pcur = head.next;
         ListNode prev = head;//找到上一个节点
         int count = 0;
         while(pcur != null) {
             if(count == pos) {
                 prev.next = newnode;
                 newnode.next = pcur;
                 return;
             }
             count++;
             prev = pcur;
             pcur = pcur.next;
         }
     }

     public void check(int pos) throws PosIllegal{
        if(pos < 0 || pos > this.size()){
            throw new PosIllegal("addIndex中的:pos不合法");
        }
     }

     public boolean contains(int val) {
        ListNode pcur = head;
        while(pcur != null) {
            if(pcur.val == val) {
                return true;
            }
            pcur = pcur.next;
        }
        return false;
     }

     public void remove(int val) {
        if(head == null) {
            return;
        }

        //删除头节点
        if(head.val== val) {
            head = head.next;
            return;
        }


        ListNode pcur = head;
        while(pcur.next != null) {
            if(pcur.next.val == val) {
                pcur.next = pcur.next.next;
                return;
            }
            pcur = pcur.next;
        }
     }


     public void removeAllkey(int val) {
         if(head == null) {
             return;
         }

         //从第二个节点开始处理
         ListNode pcur = head.next;
         ListNode prev = head;
         while(pcur != null) {
             if(pcur.val == val) {
                 prev.next = pcur.next;
             }else {
                 prev = pcur;
             }
             pcur = pcur.next;
         }

         //处理头节点
         if(head.val == val) {
             head = head.next;
         }
     }


     public void clear() {
        ListNode pcur = head;
        while(pcur != null) {
            ListNode pn = pcur.next;
            pcur.next = null;
            pcur = pn;
        }
        head = null;
     }







}
